package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /*
     * 查询通用设置
     * */
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //获取用户ID
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //获取用户手机号码
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //获取用户的通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    //设置陌生人问题
    public void saveQuestion(String content) {
        //获取ID
        Long userId = UserHolder.getUserId();
        //查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //判断是否存在
        if (question == null) {
            //如果不存在则保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //如果存在则更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /*
    * 通知设置
    * */
    public void saveSettings(Map map) {
        //获取ID
        Long userId = UserHolder.getUserId();
        //根据用户id查询设置
        Settings settings = settingsApi.findByUserId(userId);
        //判断
        if (settings==null){
            //保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification((boolean)map.get("likeNotification"));
            settings.setPinglunNotification((boolean)map.get("pinglunNotification"));
            settings.setGonggaoNotification((boolean)map.get("gonggaoNotification"));
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification((boolean)map.get("likeNotification"));
            settings.setPinglunNotification((boolean)map.get("pinglunNotification"));
            settings.setGonggaoNotification((boolean)map.get("gonggaoNotification"));
            settingsApi.update(settings);
        }
    }

    /*
    * 分页查询黑名单列表
    * */
    public PageResult blacklist(int page, int size) {
        //获取ID
        Long userId = UserHolder.getUserId();
        //调用api查询用户的黑名单
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size);
        //对象转化，将结果封装入PageResult
        PageResult pr = new PageResult(page,size, (long) iPage.getTotal(),iPage.getRecords());
        //返回结果
        return pr;
    }

    /*
    * 取消黑名单
    * */
    public void deleteBlackList(Long blackUserId) {
        //获取ID
        Long userId = UserHolder.getUserId();
        //调用api删除黑名单记录
        blackListApi.delete(UserHolder.getUserId(),blackUserId);
    }
}
